Contents | Index | < Browse | Browse >

LETTERexitULETTER Exit program.

Overview
#include <stdlib.h>

void exit(result)

int result; // return code

Portability
ANSI

Description
This function terminates the program execution. If you are eg. writing a CLI program and you wish to set a return code the program must be terminated using exit() because the startup code ignores the result of the main() function and always returns 0 to the calling instance.
Functions declared with "atexit" will be executed, destructors for globally defined variables will be called and resources (such as files opened with "fopen", memory allocated with "malloc" or "calloc" etc.) will be freed.
A result of 0 indicates that the program was executed successfully. Any other value is interpreted as an error code.
Memory regions allocated with the Exec routines ("AllocMem" etc.) and files opened with DOS routines like "Open" must be closed before calling "exit".

See also
abort , atexit , signal

Example
#include <stdlib.h>
#include <stdio.h>

void main(int argc, char *argv[])
{
FILE *f;

if(argc > 1)
{
f = fopen(argv[1],"r");
if(f == NULL)
{
fprintf(stderr,
"Can't open file \"%s\"\n,
argv[1]);
exit(100);
} fclose(f);
}
}